package com.qiang.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @title: MatcherUtils
 * @date:  2020-04-28 15:38
 * @version: V1.0
 */
public class MatcherUtils {

    public static void main(String[] args) {
        String amount = "-11111111555.22";
        System.out.println(IsMoneyAndMinus(amount));

//        System.out.println(IsMoney(amount));
    }

    /**
     * 功能描述: <br>
     * 〈验证金额,包含负数,保留2位小数〉
     * @param: [str]
     * @return: boolean
     */
    public static boolean IsMoneyAndMinus(String str) {
        String regex = "^(-?[0-9]{1,12}+)(.[0-9]{0,2})?$";
        return match(regex, str);
    }

    /**
     * 功能描述: <br>
     * 〈验证金额,正数,保留2位小数〉
     * @param: [str]
     * @return: boolean
     */
    public static boolean IsMoney(String str) {
        String regex = "^([0-9]+)(.[0-9]{2})?$";
        return match(regex, str);
    }

    //公共验证方法
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

}
